package net.hn.hnms.biz.common.activiti.builder;


import net.hn.hnms.biz.common.activiti.params.ProcessQueryParam;
import net.hn.hnms.biz.common.activiti.service.ProcessApiService;

/**
 * Created on 2022/8/5.
 *
 * @author ly
 * @desc: 流程执行器
 */
public class ProcessExecutor {
    /**
     * 流程运行实例id
     */
    private String processInstanceId;

    /**
     * 业务编码
     */
    private String businessKey;

    /**
     * 暂停原因;终止原因...
     */
    private String reason;

    private final ProcessQueryParam processQueryParam = new ProcessQueryParam();
    private ProcessApiService processApiService;

    private ProcessExecutor() {
    }

    public ProcessExecutor(ProcessApiService processApiService) {
        this.processApiService = processApiService;
    }

    /**
     * 开始流程
     * @return ProcessStarter 流程开始构建器
     */
    public ProcessStarter startProcess() {
        return new ProcessStarter(this.processApiService);
    }

    /**
     * 使用文件创建流程
     * @return BpmnFileProcessCreator 文件->流程创建器
     */
    public BpmnFileProcessCreator createProcessByFile() {
        return new BpmnFileProcessCreator(this.processApiService);
    }

    /**
     * 使用自定义流程生成数据创建流程
     * @return BpmnFileProcessCreator 自定义流程生成数据->流程创建器
     */
    public DataProcessCreator createProcessByData() {
        return new DataProcessCreator(this.processApiService);
    }

    /**
     * 创建流程查询器
     * @return ProcessQuery 流程查询器
     */
    public ProcessQuery createProcessQuery() {
        return new ProcessQuery(this.processApiService);
    }

    public ProcessExecutor processInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
        processQueryParam.setProcessInstanceId(this.processInstanceId);
        return this;
    }

    public ProcessExecutor businessKey(String businessKey) {
        this.businessKey = businessKey;
        processQueryParam.setBusinessKey(this.businessKey);
        return this;
    }

    public ProcessExecutor reason(String reason) {
        this.reason = reason;
        processQueryParam.setReason(this.reason);
        return this;
    }

    /**
     * 删除运行的流程实例
     */
    public void deleteProcess() {
        processApiService.deleteProcess(processQueryParam);
    }

    /**
     * 终止运行的流程实例
     */
    public void endProcess() {
        processApiService.endProcess(processQueryParam);
    }

    /**
     * 暂停运行的流程实例
     */
    public void suspendProcess() {
        processApiService.suspendProcess(processQueryParam);
    }

    /**
     * 激活暂停的流程实例
     */
    public void activateProcess() {
        processApiService.activateProcess(processQueryParam);
    }
}
